package com.agmte.api.common.utils;

import com.alibaba.fastjson.JSONObject;

public class JsonBuilder {
    private JSONObject json = new JSONObject();

    public static JsonBuilder first(String key, Object value) {
        return new JsonBuilder().put(key, value);
    }

    public static JSONObject pair(String key, Object value, Object... values) {
        if (values.length % 2 != 0) {
            throw new IllegalArgumentException("values length must be divisible by two");
        }
        JsonBuilder first = JsonBuilder.first(key, value);
        for (int i = 0; i < values.length; i += 2) {
            first.put((String) values[i], values[i + 1]);
        }
        return first.build();
    }

    public JsonBuilder put(String key, Object value) {
        json.put(key, value);
        return this;
    }

    public JSONObject build() {
        return json;
    }
}
